const App = {
    mixins: [Query],//配置文件，路径/utils/mixins.js
    data() {
        return {
            photo,
            page: {
                activeName: 'first',
                value1: '',
                value2: '',
                value3: '',
                value4: '',
                PageNumber: 1, //页码
                RowAmount: 20,
                Condition: window.btoa(unescape(encodeURIComponent("%%,%%,%%,%%")))
            },
            xlsx_table_type:[],
            str1: '',
            str: '',
            isType: '0',
        }
    },
    mounted() {
        this.getData();
    },
    methods: {
        check() {
            this.page.Condition = window.btoa(unescape(encodeURIComponent("%" + this.page.value1 + "%,%" + this.page.value2 + "%,%" + this.page.value3 + "%,%"+(this.page.value4 || '')+"%")));
            this.postgetList(this.page);
        },
        getData(){
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                method: "POST",
                data: {
                    Data: 'xlsx_table_type',
                    Condition: "Row_Status='0'",
                },
                success: (res) => {
                    this.xlsx_table_type = res;
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            })
        },
        handleDownload(row, is = true) {
            $.ajax({
                headers: {
                    "content-type": "application/x-www-form-urlencoded",
                    "Session": 'session5'
                },
                url: `${y}/generalQueryAllColumnByTableName`,
                method: "get",
                data: {
                    TableName: row.Xlsx_Table_SerialNumber,
                },
                success: (res) => {
                    if (res.Status == 'success') {
                        if (is) {
                            const wb = XLSX.utils.book_new();
                            const ws_data = [res.Data.filter(i => ![
                                'XLSX_SerialNumber',
                                'Creater_SerialNumber',
                                'Create_Datetime',
                                'Row_Status',
                                'Operation_ID',
                                'Operation_Datetime',
                            ].some(z => z == i.COLUMN_NAME)).map(i => i.COLUMN_COMMENT)];
                            const ws = XLSX.utils.aoa_to_sheet(ws_data);
                            ws['!cols'] = [];
                            ws_data[0].forEach(item => {
                                ws['!cols'].push({ alignment: { horizontal: "center" }, wpx: 150 });
                            })
                            XLSX.utils.book_append_sheet(wb, ws, row.Xlsx_File_Name);
                            XLSX.writeFile(wb, row.Xlsx_File_Name + "_导入模板_" + (+new Date()) + ".xlsx");
                        } else {
                            $.ajax({
                                headers: requestHeaders(),
                                url: `${mh}/generalBusiness/generalQueryByTableName`,
                                type: "POST",
                                data: {
                                    Data: row.Xlsx_Table_SerialNumber,
                                    Condition: "",
                                },
                                success: (data) => {
                                    const wb = XLSX.utils.book_new();
                                    const ws_data = [res.Data.filter(i => ![
                                        'XLSX_SerialNumber',
                                        'Creater_SerialNumber',
                                        'Create_Datetime',
                                        'Row_Status',
                                        'Operation_ID',
                                        'Operation_Datetime',
                                    ].some(z => z == i.COLUMN_NAME)).map(i => i.COLUMN_COMMENT)];
                                    data.forEach(item => {
                                        let arr = [];
                                        res.Data.filter(i => ![
                                            'XLSX_SerialNumber',
                                            'Creater_SerialNumber',
                                            'Create_Datetime',
                                            'Row_Status',
                                            'Operation_ID',
                                            'Operation_Datetime',
                                        ].some(z => z == i.COLUMN_NAME)).forEach(obj => {
                                            arr.push(item[obj.COLUMN_NAME])
                                        })
                                        ws_data.push(arr);
                                    })
                                    const ws = XLSX.utils.aoa_to_sheet(ws_data);
                                    ws['!cols'] = [];
                                    ws_data[0].forEach(item => {
                                        ws['!cols'].push({ alignment: { horizontal: "center" }, wpx: 150 });
                                    })
                                    XLSX.utils.book_append_sheet(wb, ws, row.Xlsx_File_Name);
                                    XLSX.writeFile(wb, row.Xlsx_File_Name + (+new Date()) + ".xlsx");
                                },
                                complete: (XMLHttpRequest) => {
                                    requestPretreatment(XMLHttpRequest);
                                },
                                dataType: "json",
                            })
                        }
                    }
                },
                complete: (XMLHttpRequest) => {

                },
            });
        },
        handleUpload({ raw }, row) {
            const that = this;
            var reader = new FileReader();
            reader.readAsBinaryString(raw);
            reader.onload = function (e) {
                var data = e.target.result;
                var wb = XLSX.read(data, {
                    type: 'binary' //以二进制的方式读取
                });
                var sheet = wb.Sheets[wb.SheetNames[0]];//sheet0代表excel表格中的第一页
                var json = XLSX.utils.sheet_to_json(sheet);//利用接口实现转换。
                var headers = XLSX.utils.sheet_to_json(sheet, { header: 1 })[0];//利用接口实现转换。
                $.ajax({
                    headers: requestHeaders(),
                    url: `${mh}/generalBusiness/generalQueryAllColumnByTableName`,
                    method: "get",
                    data: {
                        Data: row.Xlsx_Table_SerialNumber,
                    },
                    success: (res) => {
                        //获取上传表格字段 
                        let addArr = [];
                        let data = res.filter(i => ![
                            'XLSX_SerialNumber',
                            'Creater_SerialNumber',
                            'Create_Datetime',
                            'Row_Status',
                            'Operation_ID',
                            'Operation_Datetime',
                        ].some(z => z == i.COLUMN_NAME));
                        //校验字段是否一致
                        try {
                            let data1 = data.map(i => i.COLUMN_COMMENT);
                            if (data1.length != headers.length) {
                                that.$message({
                                    type: 'error',
                                    message: '上传失败,文件格式不对,请检查后上传'
                                });
                                return;
                            }
                            for (let i = 0; i < data1.length; i++) {
                                const item = data1[i];
                                if (!headers.some(x => x == item)) {
                                    that.$message({
                                        type: 'error',
                                        message: '上传失败,文件格式不对,请检查后上传'
                                    });
                                    return;
                                }
                            }

                        } catch (error) {
                            that.$message({
                                type: 'error',
                                message: '上传失败,文件格式不对'
                            });
                        }
                        // return
                        json.forEach(item => {
                            let obj = {};
                            res.filter(i => ![
                                'XLSX_SerialNumber',
                                'Creater_SerialNumber',
                                'Create_Datetime',
                                'Row_Status',
                                'Operation_ID',
                                'Operation_Datetime',
                            ].some(z => z == i.COLUMN_NAME)).forEach(i => {
                                obj[i.COLUMN_NAME] = item[i.COLUMN_COMMENT];
                            })
                            addArr.push(obj)
                        })

                        $.ajax({
                            headers: requestHeaders(),
                            url: `${mh}/generalBusiness/generalQueryByTableNameWithOriginal`,
                            method: "post",
                            data: {
                                Data: 'xlsx_table',
                                Condition: `Xlsx_Table_SerialNumber='${row.Xlsx_Table_SerialNumber}'`,
                            },
                            success: (xlsx_table) => {
                                xlsx_table[0].Update_Datetime = getYMDHMSDate();
                                //上传更新时间
                                let lJSONObject_DataSet = {
                                    Modify: {
                                        xlsx_table
                                    },
                                    Add: {}
                                };
                                lJSONObject_DataSet['Add'][row.Xlsx_Table_SerialNumber] = addArr;
                                let Operation_Main = JSON.stringify(deepClone(lJSONObject_DataSet));
                                lJSONObject_DataSet['Add']['base_operation_log'] = [{
                                    Operation_Type: '0',
                                    Operation_Path: that.relativePath,
                                    Operation_Main,
                                    Operation_Interface: "generalSaveByRequestBody",
                                    Operation_IP: getIpAddress()
                                }]
                                submitSave(lJSONObject_DataSet, (res) => {
                                    if (res.Status == 'success') {
                                        that.$message({
                                            type: 'success',
                                            message: '上传成功,共上传' + addArr.length + '条数据'
                                        });
                                    }
                                    that.postgetList(that.page);
                                })
                            },
                            complete: (XMLHttpRequest) => {
                                requestPretreatment(XMLHttpRequest)
                            },
                        });
                    },
                    complete: (XMLHttpRequest) => {
                        requestPretreatment(XMLHttpRequest)
                    },
                });
            }
        },
        handleShow(row) {
            window.location.href = './show.html?nodeKey=' + row.Xlsx_Table_SerialNumber + '&name=' + row.Xlsx_File_Name
        },
        //编辑按钮
        handleEdit(row) {
            let { primary_key } = JSON.parse(document.getElementById("table").getAttribute("data-options"));
            window.location.href = "./add.html?action=Modify&nodeKey=" + row[primary_key] + "&type=" + this.isType;
        },
    },
};

const { createApp } = Vue;
const app = createApp(App);
//这种方式是引入全部图标，无需单个引入
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component);
}
const ElMessage = ElementPlus.ElMessage;
app.use(ElementPlus, { locale: zhCn });
app.mount("#app");
